package com.atguigu.javase;

import java.util.*;
import java.util.stream.Collectors;



/**
 * @Author: shenhao
 * @CreatTime: 2024/10/4 18:24
 * @Version: 1.0
 * list去重方法,有5种
 */
public class ListRemoveDuplicates {
    /**
     * list去除重复元素你能想到几个方法，校招题
     * 有些面试官不让你用API，要求纯手写
     * 如果你能找到更多更好方法，请给阳哥邮件:  zzyybs@126.com
     * @param args
     */
    public static void main(String[] args) {
        //m1();
        //m2();
        //m3();
        //m4();
        m5();
    }
    /**
     * 双for循环对比
     * 通过下标获取值来对比
     */
    private static void m5() {
        List<Integer> initList = Arrays.asList(70,70,-1,5,3,3,4,4,4,4,99);
        //List<Integer> srcList = new ArrayList<>(initList);
        List<Integer> newList = new ArrayList<>(initList);
        for (int i = 0; i < newList.size() - 1; i++) {
            for (int j = newList.size()-1; j >i; j--) {
                if (Objects.equals(newList.get(j), newList.get(i))) {
                    newList.remove(j);
                }
            }
        }
        System.out.println(newList);
    }

    /**
     * 循环坐标去除重复 类似双指针, 按照值找到下标
     * 如果下标不同,标识有重复需要删除
     */
    private static void m4() {
        List<Integer> initList = Arrays.asList(70,70,-1,5,3,3,4,4,4,4,99);
        List<Integer> srcList = new ArrayList<>(initList);
        List<Integer> newList = new ArrayList<>(initList);
        /*Iterator<Integer> iterator = srcList.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            if (srcList.indexOf(next)!=srcList.lastIndexOf(next)){
                iterator.remove();
            }
        }*/
        for (Integer element : srcList) {
            if (newList.indexOf(element)!= newList.lastIndexOf(element)){
                newList.remove(newList.lastIndexOf(element));
            }
        }

        System.out.println(srcList);
        System.out.println(newList);
    }

    /*
    * Stream流式计算*/
    private static void m3(){
        List<Integer> initList = Arrays.asList(70,70,-1,5,3,3,4,4,4,4,99);
        List<Integer> srcList = new ArrayList<>(initList);
        List<Integer> newList = new ArrayList<>();
        //srcList.stream().distinct().forEach(newList::add);
        newList = srcList.stream().distinct().collect(Collectors.toList());
        System.out.println(newList);
    }
    /**
     * HashSet或者LinkedHashSet去重复
     */
    private static void m2() {
        List<Integer> initList = Arrays.asList(70,70,-1,5,3,3,4,4,4,4,99);
//        LinkedHashSet<Integer> newSet = new LinkedHashSet<>(initList);
        List<Integer> destList = new ArrayList<>(new HashSet<>(initList));
        System.out.println(destList);
        System.out.println();
        System.out.println();
        //linkedHashSet可以保证是原来的顺序
        destList = new ArrayList<>(new LinkedHashSet<>(initList));
        System.out.println(destList);

    }

    /*
    * for循环遍历判断是否含有,没有就新增到newList里 单层for循环
    * */
    private static void m1() {
        List<Integer> initList = Arrays.asList(70,70,-1,5,3,3,4,4,4,4,99);
        List<Integer> srcList = new ArrayList<>(initList);
        List<Integer> newList = new ArrayList<>();
        //for (Integer result : srcList) {
        for (int i = 0; i < srcList.size(); i++) {
            Integer result = srcList.get(i);
            if (!newList.contains(result)) {
                newList.add(result);
            }
        }
        System.out.println(newList);
    }
}
